package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.TipoTelefono;
import services.TipoTelefonoService;

@FacesConverter("tipotelefonoConverter")
public class TipoTelefonoConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            TipoTelefonoService service = (TipoTelefonoService) fc.getExternalContext().getApplicationMap().get("tipoTelefonoService");
            Iterator<TipoTelefono> i=service.getListaTiposTelefonos().iterator();
            while (i.hasNext()) {
				TipoTelefono t = (TipoTelefono) i.next();
				if(t.getIdTipoTelefono() == Integer.parseInt(value))
					return t;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((TipoTelefono) object).getIdTipoTelefono());
        }
        else {
            return null;
        }
    }  
}
